using System;
using Slaks.DocumentSerialization.Dictionary;
using Slaks.DocumentSerialization.Document;
using Slaks.VectorBasics.VectorBuilder.Functors;

namespace Slaks.VectorBasics.VectorBuilder.ExperimentalRepresentation
{
	public class StandardSentenseWeightRepresentation
	{
		public static Vector Build(Document document, DictionaryMap dictionaryMap)
		{
			string documentName = document.FileName;
			int index = documentName.LastIndexOfAny(new char[] {'\\', '/'});
			if (index >= 0 && (index + 1 < documentName.Length)) documentName = documentName.Substring(index + 1);
			Vector vector = new Vector(documentName);

			VectorItem vectorItem;
			foreach (Node node in document.Nodes)
			{
				if (dictionaryMap.Contains(node.Id))
				{
					vectorItem = vector.CreateAddVectorItem(node.Id);

					long minSentenceNumber = node.GetMinSentenceNumber();
					vectorItem.Frequency = (double)
						node.TotalTermFrequency  *
						(new SentenceWeightFunctor(minSentenceNumber)).GetNodeWeight() / document.UniqueTermCount;
				}

			}

			if (vector.GetVectorLength() == 0) throw new VectorException(String.Format("Length of the vector {0} is zero", documentName));

			return vector;
		}
	}
}